package org.csu.wuliu.service;

import org.csu.wuliu.domain.Order;
import org.csu.wuliu.persistence.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;

//    public List<Order> getOrders(){
//        return orderMapper.getOrders();
//    }

    public Order getOrder(String orderId){
        return orderMapper.getOrderByOrderId(orderId);
    }

//    public List<Order> searchOrderList(String keyword){
//        return orderMapper.searchOrder("%" + keyword.toLowerCase() + "%");
//    }

    public List<Order> getOrdersByUsername(String username){
        return orderMapper.getOrderListByUsername(username);
    }

    public void insertOrder(Order order){
       orderMapper.insertOrder(order);
    }

    public void updateOrder(Order order){

        orderMapper.updateOrder(order);
    }

    public List<Order> getOrdersByOrderStatus(int status){
        return orderMapper.getOrderListByStatus(status);

    }

    public List<Order> getOrdersByOrderDate(String date){
        return  orderMapper.getOrderListByDate(date);
    }

    public List<Order> getOrders(){
        return orderMapper.getOrders();
    }

    //根据时间生成订单单号
    public String autoCreated(){
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateStr = formatter.format(date);
         String orderId="OD"+dateStr.substring(0,dateStr.length()-2);
         return orderId;
    }

    //自动匹配对应状态
    public String autoMatched(int status){
        switch (status){
            case 0: return "缺货";
            case 1: return "进货中";
            case 2:return "有货";
            case 3: return "已从总站发出";
            case 4:return "到达分站";
            case 5:return "配送中";
            case 6:return "用户已签收";
            case 7: return "用户在缺货时退货";
            case 8: return "用户在进货中时退货";
            case 9:return "用户在有货时退货";
            case 10:return "用户在已从总站发出时退货";
            case 11:return "用户在到达分站时退货";
            case 12:return "用户在配送中退货";
            case 13:return "用户已签收后退货";
            default: return "异常状态";
        }
    }

}
